package cn.initcap.algorithm.sort;

import java.util.Arrays;

/**
 * @author initcap
 * @date Created in 2018/6/20 PM6:02.
 */
public class Main {

    /**
     * 比较SelectionSort和InsertionSort两种排序算法的性能效率
     * 优化后，插入排序比选择排序性能略好
     * 对于有序性强的数组，插入排序远远优于选择排序
     *
     * @param args
     */
    public static void main(String[] args) {

        int n = 1000;

        // 测试1 一般测试
        System.out.println("Test for random array, size = " + n + " , random range [0, " + n + "]");

        Integer[] arr1 = SortTestHelper.generateRandomArray(n, 0, n);
        Integer[] arr2 = Arrays.copyOf(arr1, arr1.length);
        Integer[] arr3 = Arrays.copyOf(arr1, arr1.length);
        Integer[] arr4 = Arrays.copyOf(arr1, arr1.length);
        Integer[] arr5 = Arrays.copyOf(arr1, arr1.length);

        SortTestHelper.testSort("cn.initcap.algorithm.sort.n2.SelectionSort", arr1);
        SortTestHelper.testSort("cn.initcap.algorithm.sort.n2.InsertionSort", arr2);
        SortTestHelper.testSort("cn.initcap.algorithm.sort.n2.BubbleSort2", arr3);
        SortTestHelper.testSort("cn.initcap.algorithm.sort.n2.ShellSort", arr4);
        SortTestHelper.testSort("cn.initcap.algorithm.sort.nlogn.MergeSort", arr5);

        System.out.println();


        // 测试2 有序性更强的测试
        System.out.println("Test for more ordered random array, size = " + n + " , random range [0,3]");

        arr1 = SortTestHelper.generateRandomArray(n, 0, 3);
        arr2 = Arrays.copyOf(arr1, arr1.length);
        arr3 = Arrays.copyOf(arr1, arr1.length);
        arr4 = Arrays.copyOf(arr1, arr1.length);

        SortTestHelper.testSort("cn.initcap.algorithm.sort.n2.SelectionSort", arr1);
        SortTestHelper.testSort("cn.initcap.algorithm.sort.n2.InsertionSort", arr2);
        SortTestHelper.testSort("cn.initcap.algorithm.sort.n2.BubbleSort", arr3);
        SortTestHelper.testSort("cn.initcap.algorithm.sort.n2.ShellSort", arr4);

        System.out.println();


        // 测试3 测试近乎有序的数组
        int swapTimes = 100;
        System.out.println("Test for nearly ordered array, size = " + n + " , swap time = " + swapTimes);

        arr1 = SortTestHelper.generateNearlyOrderedArray(n, swapTimes);
        arr2 = Arrays.copyOf(arr1, arr1.length);
        arr3 = Arrays.copyOf(arr1, arr1.length);
        arr4 = Arrays.copyOf(arr1, arr1.length);

        SortTestHelper.testSort("cn.initcap.algorithm.sort.n2.SelectionSort", arr1);
        SortTestHelper.testSort("cn.initcap.algorithm.sort.n2.InsertionSort", arr2);
        SortTestHelper.testSort("cn.initcap.algorithm.sort.n2.BubbleSort", arr3);
        SortTestHelper.testSort("cn.initcap.algorithm.sort.n2.ShellSort", arr4);
    }

}
